<?php
/**
 * Created by Boume.
 * Date: 5/4 0004
 * Time: 2:32
 */
namespace app\admin\controller;
use app\admin\model\Admin;
use app\admin\model\Auth;
use think\facade\Validate;

class Role extends Base
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $Role = new \app\admin\model\Role();
            $lists = $Role->withCount('Admin')->order('id asc')->select();
            return json([
                'code' => 0,
                'msg' => '',
                'count' => '',
                'data' => $lists
            ]);
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $Role = new \app\admin\model\Role();
            $post = input('post.');
            $rule = [
                'name' => 'require|unique:role,name',
                'lock_msg' => 'requireIf:lock,1',
            ];
            $msg = [
                'name.require' => '角色名称必填',
                'name.unique' => '角色名称已存在',
                'lock_msg.requireIf' => '状态锁定,备注必填',
            ];
            $validate = Validate::make($rule, $msg);
            $result = $validate->check($post);
            if (!$result) $this->error($validate->getError());
            if($post['lock']==0) $post['lock_msg'] = "";
            $Role->allowField(true)->save($post);
            return json([
                'code' => 1,
                'msg' => '添加成功'
            ]);
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        $id = input('param.id', 0, 'intval');
        $Role = new \app\admin\model\Role();
        if ($this->request->isPost()) {
            $post = input('post.');
            $rule = [
                'name' => 'require|unique:role,name,' . $id,
                'lock_msg' => 'requireIf:lock,1',
            ];
            $msg = [
                'name.require' => '角色名称必填',
                'name.unique' => '角色名称已存在',
                'lock_msg.requireIf' => '状态锁定,备注必填',
            ];
            $validate = Validate::make($rule, $msg);
            $result = $validate->check($post);
            if (!$result) $this->error($validate->getError());
            if($post['lock']==0) $post['lock_msg'] = "";
            $Role->allowField(true)->save($post, [
                'id' => $id
            ]);
            return json([
                'code' => 1,
                'msg' => '修改成功'
            ]);
        } else {
            $data = $Role->where('id', $id)->find();
            if(empty($data)) $this->error('角色不存在');
            $this->assign('data', $data->getData());
            return $this->fetch();
        }
    }

    public function del()
    {
        $id = input('param.id', 0, 'intval');
        if ($id == 1) $this->error('系统角色无法删除');
        if (Admin::where('role_id', $id)->count()) $this->error('角色下有用户,无法删除');
        \app\admin\model\Role::destroy($id);
        $Auth = new Auth();
        $Auth->where('rid', $id)->delete();
        return json([
            'code' => 1,
            'msg' => '删除成功'
        ]);
    }

    public function auth()
    {
        $id = input('param.id', 0, 'intval');
        $Auth = new Auth();
        if ($this->request->isPost()) {
            if ($id == 1) $this->error('系统角色,无需设置');
            $post = input('post.');
            $Auth->where('rid', $id)->delete();
            if (!empty($post['nid'])) {
                foreach ($post['nid'] as $v) {
                    if ($v == 0) continue;
                    $data[] = [
                        'rid' => $id,
                        'nid' => $v
                    ];
                }
                $Auth->saveAll($data);
            }
            return json([
                'code' => 1,
                'msg' => '权限设置成功',
            ]);
        } else {
            $nids = $Auth->where('rid', $id)->column('nid');
            $nodes = \app\admin\model\Node::all();
            $nodes = $nodes->toArray();
            $root = false;
            foreach ($nodes as $k => $v) {
                if (in_array($v['id'], $nids)) $root = $nodes[$k]['checked'] = true;
            }
            array_push($nodes, [
                'id' => 0,
                'name' => '根系统',
                'checked' => $root
            ]);
            $this->assign('node', json_encode($nodes));
            return $this->fetch();
        }
    }
}